#include "stdafx.h"
#include "MarketDataLinkFactory.h"

#include "config.h"
#include "LogFile.h"


static CtpMarketDataLink* CreateCtpMarketDataLink() {
	LogFile::PrintLine("Creating CTP market data link now");
	Config *cfg = Config::getInstance();

	string mdApiUrl;
	if(!cfg->getValue("MdApiUrl", &mdApiUrl)) {
		LogFile::PrintLine("Cannot find market data api url", false);
	}
	char addr[512];
	sprintf(addr, "%s", mdApiUrl.c_str());
	CThostFtdcMdApi *api = CThostFtdcMdApi::CreateFtdcMdApi(cfg->getValue("MdApiPath", "zzMdApi.con").c_str());
	CtpMarketDataLink *marketDataLink = new CtpMarketDataLink("md", api);
	marketDataLink->loadSettings();
	api->RegisterSpi( marketDataLink );
	api->RegisterFront(addr);
	LogFile::PrintLine("Market data link created");
	return marketDataLink;
}

MarketDataLinkFactory* MarketDataLinkFactory::Instance = 0;

MarketDataLinkFactory::MarketDataLinkFactory() {
}


MarketDataLinkFactory::~MarketDataLinkFactory() {
}

void MarketDataLinkFactory::Create() {
	if(Instance == 0) {
		Instance = new MarketDataLinkFactory();
	}
}


MarketDataLink* MarketDataLinkFactory::GetLink() {
	Config *cfg = Config::getInstance();
	string linkType = cfg->getValue("MarketDataLinkType", "");
	if(linkType == "ctp") {
		return CreateCtpMarketDataLink();
	} else {
		return 0;
	}
}